package ihm.table;

import java.util.HashSet;
import java.util.Set;

import javax.swing.table.AbstractTableModel;

/**
 * TableModel.
 * @author Guillaume Pocas, Jean-François Augé & Sun Microsystem
 */
@Deprecated
public class AppletTableModel extends AbstractTableModel {

	private static final long serialVersionUID = 1L;

	private String[] columnNames;
	private Object[][] data;
	private Set<Integer> locked_col;

	/**
	 * Constructeur.
	 * @param columnNames
	 * @param data
	 */
	public AppletTableModel(String[] columnNames, Object[][] data) {
		this.columnNames = columnNames;
		this.data = data;
		this.locked_col = new HashSet<Integer>();
	}
	
	/**
	 * Constructeur.
	 * @param columnNames
	 */
	public AppletTableModel(String[] columnNames) {
		this.columnNames = columnNames;
		this.locked_col = new HashSet<Integer>();
	}

	/**
	 * Verrouille une colonne du modèle.
	 * @param col
	 */
	public void lockColumn(int col) {
		locked_col.add(col);
	}
	
	/**
	 * Met à jour les champs selectionnés.
	 * @param data
	 * @param firstRow
	 * @param lastRow
	 */
	public void updateInserts(Object[][] data, int firstRow, int lastRow) {
		this.data = data;
		fireTableRowsInserted(firstRow, lastRow);
	}
	
	/**
	 * Met à jour les champs selectionés.
	 * @param data
	 * @param firstRow
	 * @param lastRow
	 */
	public void updateChanges(Object[][] data, int firstRow, int lastRow) {
		this.data = data;
		fireTableRowsUpdated(firstRow, lastRow);
	}
	
	/**
	 * Met à jour le modèle.
	 * @param data
	 */
	public void updateModel(Object[][] data) {
		this.data = data;
		System.err.println(data.length);
		fireTableDataChanged();
	}
	
	/**
	 * Met à jour le modèle.
	 */
	public void updateModel() {
		fireTableDataChanged();
	}
	
	public int getColumnCount() { return columnNames.length; }

	public int getRowCount() { return data.length; }

	public String getColumnName(int col) { return columnNames[col]; }

	public Object getValueAt(int row, int col) { return data[row][col]; }

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); }

	/*
	 * Don't need to implement this method unless your table's
	 * editable.
	 */
	public boolean isCellEditable(int row, int col) {
		//Note that the data/cell address is constant,
		//no matter where the cell appears onscreen.
		if(locked_col.contains(col))
			return false;
		else
			return true;
	}

	/*
	 * Don't need to implement this method unless your table's
	 * data can change.
	 */
	public void setValueAt(Object value, int row, int col) {
		data[row][col] = value;
		//fireTableCellUpdated(row, col);
	}
	
	public void setData(Object[][] data) {
		this.data = data;
	}

}
